import React from "react";
import BaseModal from "@biz/BaseModal";
import {LdhFormItem, LdhInput} from "@biz/Components";

const prefix = "withdrawal-modal";
export default class  WithdrawalModal extends BaseModal {

    constructor(props) {
        super(props);
        this.visibleStatus = "withdrawal";
        this.title = "申请提现";
        this.className = prefix;
        this.width = 400;
        this.noOp= true;

    }

    componentWillReceiveProps(nextProps, nextContext) {
        let {modalStatus, withdrawalData} = nextProps.store;
        let oldModalStatus = this.props.store.modalStatus;

        if(modalStatus && modalStatus!=oldModalStatus){
            let {shopId, shopName, withdrawableAmount} = withdrawalData;
            this.title = `申请提现 ${shopName} (${withdrawableAmount})`
            setTimeout(()=>{
                this.form().setFieldsValue({shopId, shopName});
            },100)

        }else{
            this.title = `申请提现`
        }
    }

    onSubmit(values){
        let {action, store:{withdrawalData}} = this.props;
        let vo = {...values, shopId:withdrawalData.shopId}

        action.createWithdrawal(vo, ()=>{
            this.onCancel();
        });

    }
    onCancel(){
        let {action} = this.props;
        action.cancelModal();
    }

    renderDetail() {
        let {withdrawalData={}} = this.props.store;
        return <div>
            <LdhFormItem label="门店名称" name="shopName" required>
                <div>{withdrawalData.shopName}</div>
            </LdhFormItem>
            <LdhFormItem label="提现金额" name="amount" required
                         rules={[
                             {pattern:window.CONFIG.REG_NUM_D2, message:"金额保留两位小数"},
                             {
                                 validator: (rule, newValue, callback) => {
                                     if (newValue != "" && newValue != undefined && Number(newValue) < 0) {
                                         return Promise.reject("提现金额要大于0");
                                     }
                                     if (newValue != "" && newValue != undefined && Number(newValue) > withdrawalData.withdrawableAmount) {
                                         return Promise.reject(`提现金额要小于账户可提现金额（${withdrawalData.withdrawableAmount}）`);
                                     }
                                     return Promise.resolve();
                                 }
                             }
                         ]}>
                <LdhInput />
            </LdhFormItem>

        </div>
    }

}